//
//  UIActionSheet+RAC.m
//  高仿暴走斗图
//
//  Created by key on 16/3/22.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "UIActionSheet+RAC.h"

@implementation UIActionSheet (RAC)

+ (void)showSheetNextBlock:(void(^)(id x))nextBlock showInView:(UIView *)view withTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray<NSString *> *)otherButtonTitles {
    
    [UIActionSheet showSheetNextBlock:^(id x) {
        if (nextBlock) {
            nextBlock(x);
        }
    } showInView:view withTitle:title delegate:nil cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:nil];
}

+ (void)showSheetNextBlock:(void(^)(id x))nextBlock showInView:(UIView *)view withTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray<NSString *> *)otherButtonTitles {
    
    [[[UIActionSheet rac_showSheetInView:view withTitle:title delegate:delegate cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:otherButtonTitles] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
        if (nextBlock) {
            nextBlock(x);
        }
    }];
}

+ (RACSignal *)rac_showSheetInView:(UIView *)view withTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray<NSString *> *)otherButtonTitles {
    
    return [UIActionSheet rac_showSheetInView:view withTitle:title delegate:nil cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:otherButtonTitles];
}

+ (RACSignal *)rac_showSheetInView:(UIView *)view withTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray<NSString *> *)otherButtonTitles {
    
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:title delegate:delegate cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:nil];
    
    for (NSString *other in otherButtonTitles) {
        [sheet addButtonWithTitle:other];
    }
    [sheet showInView:view];
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[[sheet rac_buttonClickedSignal] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            [subscriber sendNext:x];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}

@end
